/*
 * Copyright (c) 2008 Eric Berry <elberry@gmail.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package pulpcore.utils;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 *
 * @author elberry
 */
public abstract class ProjectUtils {

	  public static enum Type {

			 Full, Quick;
	  }

	  private ProjectUtils() {

	  }

	  public static void fixProjectFiles(File projectDirectory, String projectName, ProjectUtils.Type projectType) throws Exception {
			 cleanProjectXML(projectDirectory, projectName, projectType);
	  }

	  private static void cleanProjectXML(File projectDirectory, String projectName, ProjectUtils.Type projectType) throws IOException {
			 File nbprojectDirectory = new File(projectDirectory, "nbproject");
			 File projectXML = new File(nbprojectDirectory, "project.xml");
			 if (projectXML != null && projectXML.exists()) {
					// TODO: This should be changed over to use NIO.
					// read the project.xml and replace the word "Full" with the project name.
					FileReader reader = new FileReader(projectXML);
					StringBuilder builder = new StringBuilder();
					int read = reader.read();
					while (read >= 0) {
						builder.append((char) read);
						read = reader.read();
					}
					String fileContents = builder.toString();
					//System.out.println("pre replacement:\n" + fileContents);
					fileContents = fileContents.replaceAll(projectType.toString(), projectName);
					//System.out.println("post replacement:\n" + fileContents);
					reader.close();
					// write out the new charBuffer.
					FileWriter writer = new FileWriter(projectXML, false);
					writer.write(fileContents);
					writer.flush();
					writer.close();
			 }
	  }
}
